/** 
 * @(#)DVMRPRoutingTableWindow.java
 */

/**
 * @author Robert Denda&Didier Gutacker
 * @version 1.1
 * @see RoutingTableWindow 
 * @see Network
 * @see Node
 *
 * original author Robert Denda, changes made by Didier Gutacker
 */

import java.awt.*;
import java.util.Vector;

public class DVMRPRoutingTableWindow extends RoutingTableWindow {
 
  public DVMRPRoutingTable table;
  public Node n;

  public DVMRPRoutingTableWindow( DVMRPRoutingTable table, Node n, Network net ) {
    super( net );
    this.n = n;
    this.net = net;
    this.table = table;
    textArea = new TextArea( 10, 66 );
    textArea.setEditable( false );
    add( "Center", textArea );
    dismissButton = new Button( text.buttonPanel_dismissButtonText );
    add( "South", dismissButton );
    pack();
    show();
    repaint();
  } // DVMRPRoutingTableWindow.DVMRPRoutingTableWindow

  public boolean action( Event e, Object obj ) {
    if ( obj.equals( text.buttonPanel_dismissButtonText ) ) {
      dispose();
      table.tableWindowDone();
      table = null;
      n = null;
      text = null;
      thread.stop();
      thread = null;
      return true;
    } // if
    return false;
  } // DVMRPRoutingTableWindow.action

  synchronized public void run() {
    for ( ;; ) {
      try {
	wakeUpAble = true;
	wait();
      } catch( InterruptedException e ) {
      } // try 
      wakeUpAble = false;
      text = net.getSimulation().getText();
      setTitle( "Routing Table " + text.ofText + " " + text.nodeTitleText + " " + n.getID() );
      Vector entries = table.getEntries();
      textArea.setText( text.nodeTitleText + "  \t " + text.linkTitleText + "  \t " + text.distanceText + " \t " +text.forwardToText + "\n\n" );
      for ( int i = 0; i < entries.size(); i += 1 ) {
	RoutingEntry entry = ( RoutingEntry ) entries.elementAt( i );
	textArea.appendText( text.nodeTitleText + " " + entry.destinationID + "\t " + text.linkTitleText + " "+ entry.l.getID() + "\t " );
	if ( entry.metric >= entry.infinity ) {
	  textArea.appendText( "infinity\t" );
	} else {
	  textArea.appendText( "" + entry.metric + "\t\t " );
	} // if
	Vector childList = ((DVMRPRoutingEntry) entry).getChildList();
	
	if ( childList != null ) {
	  for ( int j = 0; j < childList.size(); j += 1 ) {
	    Link l = (Link) childList.elementAt(j);
	    textArea.appendText( text.linkTitleText.substring(0, 1) + l.getID() + " " );
	  } // for
	} // if
	textArea.appendText( "\n" );
      } // for
    } // for
    
  } // DVMRPRoutingTableWindow.run
 
} // DVMRPRoutingTableWindow
